﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;

namespace CNRegex.Web.Utility
{
	public enum ButtonType
	{
		删除,
		新增,
		编辑,
		导出,
		条件查询
	}

	/// <summary>
	/// 按钮扩展方法
	/// </summary>
	public static class ControlExtension
	{
		private const string createIconName = "ToolBarAdd.png";
		private const string deleteIconName = "ToolBarAdd.png";
		private const string editIconName = "ToolBarAdd.png";
		private const string exportIconName = "toolBarExport.gif";
		private const string scanIconName = "scan.gif";

		/// <summary>
		/// 按钮
		/// </summary>
		/// <param name="htmlHelper"></param>
		/// <param name="onclick"></param>
		/// <param name="buttonType">按钮类型</param>
		/// <returns></returns>
		public static MvcHtmlString Button(this HtmlHelper htmlHelper, ButtonType buttonType, string onclick)
		{
			return ButtonHelper(htmlHelper,buttonType,onclick, null, null);
		}

		public static MvcHtmlString Button(this HtmlHelper htmlHelper, ButtonType buttonType, string onclick, string buttonText)
		{
			return ButtonHelper(htmlHelper, buttonType,onclick,buttonText, null);
		}

		public static MvcHtmlString Button(this HtmlHelper htmlHelper, ButtonType buttonType, string onclick, object htmlAttributes)
		{
			return ButtonHelper(htmlHelper, buttonType,onclick,null, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
		}

		public static MvcHtmlString Button(this HtmlHelper htmlHelper, ButtonType buttonType, string onclick, string buttonText, object htmlAttributes)
		{
			return ButtonHelper(htmlHelper, buttonType,onclick,buttonText, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
		}

		private static MvcHtmlString ButtonHelper(HtmlHelper htmlHelper, ButtonType buttonType, string onclick, string buttonText, IDictionary<string, object> htmlAttributes)
		{
			TagBuilder aBuilder = new TagBuilder("a");
			TagBuilder imgBuilder = new TagBuilder("img");
			TagBuilder spanBuilder = new TagBuilder("span");
			if(htmlAttributes!=null)
				aBuilder.MergeAttributes(htmlAttributes);
			aBuilder.MergeAttribute("href", string.Format("javascript:{0}", onclick));
			aBuilder.MergeAttribute("style", "color:black;text-decoration: none;");
			aBuilder.MergeAttribute("class", "toolbar-button");
			switch (buttonType)
			{
				case ButtonType.删除:
					imgBuilder.MergeAttribute("src", "/images/" + deleteIconName);
					imgBuilder.MergeAttribute("alt", ButtonType.删除.ToString());
					break;
				case ButtonType.新增:
					imgBuilder.MergeAttribute("src", "/images/" + createIconName);
					imgBuilder.MergeAttribute("alt", ButtonType.新增.ToString());
					break;
				case ButtonType.编辑:
					imgBuilder.MergeAttribute("src", "/images/" + editIconName);
					imgBuilder.MergeAttribute("alt", ButtonType.编辑.ToString());
					break;
				case ButtonType.导出:
					imgBuilder.MergeAttribute("src", "/images/" + exportIconName);
					imgBuilder.MergeAttribute("alt", ButtonType.导出.ToString());
					break;
				case ButtonType.条件查询:
					imgBuilder.MergeAttribute("src", "/images/" + scanIconName);
					imgBuilder.MergeAttribute("alt", ButtonType.条件查询.ToString());
					break;
				default:
					break;
			}
			spanBuilder.InnerHtml = string.Format("{0}{1}", imgBuilder.ToString(), buttonText == null ? "&nbsp;" + buttonType.ToString() : "&nbsp;"+buttonText);
			aBuilder.InnerHtml = spanBuilder.ToString();
			return new MvcHtmlString(aBuilder.ToString());
		}

		public static MvcHtmlString ThOrderHelper<T>(this HtmlHelper htmlHelper, Expression<Func<T, object>> propertyExpression,string headerText, object thAttributes = null, object aAttributes = null)
		{
			var dictionaryThAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(thAttributes);
			var dictionaryaAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(aAttributes);
			MemberExpression memberExpr = null;
			if (propertyExpression.Body.NodeType == ExpressionType.Convert)
				memberExpr = ((UnaryExpression)propertyExpression.Body).Operand as MemberExpression;
			else if (propertyExpression.Body.NodeType == ExpressionType.MemberAccess)
				memberExpr = propertyExpression.Body as MemberExpression;
			TagBuilder thTagBuilder = new TagBuilder("th");
			TagBuilder aTagBuilder = new TagBuilder("a");
			if (thAttributes != null)
				thTagBuilder.MergeAttributes(dictionaryThAttributes);
			if (aAttributes != null)
				aTagBuilder.MergeAttributes(dictionaryaAttributes);
			aTagBuilder.MergeAttribute("href", string.Format("javascript:tableFieldOrder('{0}');", memberExpr.Member.Name));
			aTagBuilder.SetInnerText(headerText);
			thTagBuilder.MergeAttribute("data-order-field", memberExpr.Member.Name);
			thTagBuilder.InnerHtml = aTagBuilder.ToString();
			return new MvcHtmlString(thTagBuilder.ToString());
		}

	}
}